﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace Afa.Web.Mvc.Results
{
    #region 基础类 NetJsonResult
    /// <summary>
    /// 基础类
    /// </summary>
    public class NetJsonResult : JsonResult
    {
        public static JsonSerializerSettings Settings { get; private set; }

        public NetJsonResult()
        {
            this.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
        }

        public NetJsonResult(object data)
        {
            this.Data = data;
            this.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
        }

        static NetJsonResult()
        {
            Settings = new JsonSerializerSettings
            {
                //这句是解决问题的关键,也就是json.net官方给出的解决配置选项.                 
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                DateFormatString = "yyyy-MM-dd HH:mm:ss",
                ContractResolver = new DefaultContractResolver()
            };
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");
            if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
                throw new InvalidOperationException("JSON GET is not allowed");
            HttpResponseBase response = context.HttpContext.Response;
            response.ContentType = string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType;
            if (this.ContentEncoding != null)
                response.ContentEncoding = this.ContentEncoding;
            if (this.Data == null)
                return;
            var scriptSerializer = JsonSerializer.Create(Settings);
            using (var sw = new StringWriter())
            {
                scriptSerializer.Serialize(sw, this.Data);
                response.Write(sw.ToString());
            }
        }
    }
    #endregion

    #region 消息响应类
    /// <summary>
    /// 消息响应类
    /// </summary>
    public class ResponseMessage
    {
        public int code { get; set; }
        public string msg { get; set; }
        public object data { get; set; }
    }
    #endregion


    #region 获取数据结果 DataJson
    /// <summary>
    /// 操作成功结果
    /// </summary>
    public class DataJson : NetJsonResult
    {
        //private static DataJson _instance;
        //public static DataJson Instance => _instance ?? (_instance = new DataJson());

        public DataJson()
            : base()
        {
            this.Data = new ResponseMessage() { code = 1, msg = "" };
        }

        public DataJson(object data)
        {
            this.Data = new ResponseMessage() { code = 1, data = data, msg = "" };
        }
        public DataJson(object data, string msg)
        {
            this.Data = new ResponseMessage() { code = 1, data = data, msg = msg };
        }
    }
    #endregion

    #region 操作成功结果 SuccJson
    /// <summary>
    /// 操作成功结果
    /// </summary>
    public class SuccJson : NetJsonResult
    {
        //private static SuccJson _instance;
        //public static SuccJson Instance => _instance ?? (_instance = new SuccJson());

        public SuccJson()
            : base()
        {
            this.Data = new ResponseMessage() { code = 1, msg = "操作成功！" };
        }

        public SuccJson(string msg)
        {
            this.Data = new ResponseMessage() { code = 1, msg = msg };
        }
    }
    #endregion


    #region 操作失败结果 ErrorJson
    /// <summary>
    /// 操作失败结果
    /// </summary>
    public class ErrorJson : NetJsonResult
    {
        public ErrorJson()
            : base()
        {
            ResponseMessage message = new ResponseMessage()
            {
                code = -1,
                msg = "操作失败！"
            };
            this.Data = message;
        }

        public ErrorJson(string msg)
            : this(-1, msg)
        {

        }


        public ErrorJson(int code, string msg)
            : base()
        {
            ResponseMessage message = new ResponseMessage()
            {
                code = code,
                msg = msg
            };
            this.Data = message;
        }
    }
    #endregion
}
